package com.zhangxi.controller.user;

import com.zhangxi.result.Result;
import com.zhangxi.service.DishService;
import com.zhangxi.vo.DishVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhangxi
 * @create 2024-04-24 19:13
 */
@RestController("userDishController")
@Api(tags = "C端菜品管理")
@RequestMapping("/user/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据分类id查询菜品
     * @param categoryId 分类id
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result list(Long categoryId){


        //Redis KEYS
        String key = "dish_" + categoryId;

        //查询redis中是否存在菜品数据
        List<DishVo> list = (List<DishVo>) redisTemplate.opsForValue().get(key);
        if(list != null && list.size() > 0){
            //如果存在，直接返回，无须查询数据库
            return Result.success(list);
        }

        list = dishService.getByCategoryId(categoryId);

        redisTemplate.opsForValue().set(key, list);


        return Result.success(list);

    }

}
